<?php
/*
Plugin Name: dKret Meta widget
Description: Adds a sidebar widget to display Meta information.
Author: J. Kretzschmar
Version: 1.0
Author URI: http://diekretzschmars.de
*/

function widget_dkret_meta($args) {
	extract($args);
	$options = get_option('widget_dkret_meta');
	$title = empty($options['title']) ? __('Login/Register','dkret') : $options['title'];
	global $user_ID, $user_identity;
	get_currentuserinfo();
	if (!$user_ID):
		echo "\t\t\t$before_widget\n";
		echo "\t\t\t\t$before_title".$title.$after_title;
		echo "\t\t\t\t"; wp_register('<ul><li>','</li></ul>'); echo "\n";
      if (($options['dkretlogin']) != '') { ?>
				<form id="loginform" action="<?php echo get_option('siteurl'); ?>/wp-login.php" method="post">
					<div>
						<label for="log"><?php _e('Username:','dkret'); ?></label><br />
						<input type="text" name="log" id="log" size="19" /><br />
						<label for="pwd"><?php _e('Password:','dkret'); ?></label><br />
						<input type="password" name="pwd" id="pwd" size="19" /><br />
						<input type="hidden" name="rememberme" value="forever" />
						<input type="submit" name="submit" value="<?php _e('Login!','dkret'); ?>" />
						<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>"/>
					</div>
				</form>
      <?php } else { ?>
			<ul><li><?php wp_loginout(); ?></li></ul>
      <?php }
	echo "\n\t\t\t$after_widget";
  
	else:
		echo "\t\t\t$before_widget\n";
		echo "\t\t\t\t$before_title$user_identity$after_title";
		echo "\t\t\t\t<ul>\n";
		echo "\t\t\t\t\t"; wp_register();
		echo "\n\t\t\t\t\t"; ?><li><a href="<?php echo get_option('siteurl') . '/wp-login.php?action=logout&amp;redirect_to=' . $_SERVER['REQUEST_URI']; ?>"><?php _e('logout','dkret'); ?></a></li>
<?php
		echo "\t\t\t\t</ul>";
		echo "\n\t\t\t$after_widget";
	endif;
}

// Admin section
function widget_control_dkret_meta() {
 	global $wpdb;
 	$options = get_option('widget_dkret_meta');
    		
	if ( $_POST['dkret-submit'] ) {

		$options['title'] = strip_tags(stripslashes($_POST['dkret-meta-title']));
		$options['dkretlogin'] = isset($_POST['dkret-meta-login']);
		update_option('widget_dkret_meta', $options);
	}
	
	$title = htmlspecialchars($options['title'], ENT_QUOTES);
	$dkretlogin = $options['dkretlogin'] ? 'checked="checked"' : '';
	
	// The Box content
	echo '<p style="text-align:right;"><label for="dkret-meta-title" />' . __('Title:') . ' <input style="width: 200px;" id="dkret-meta-title" name="dkret-meta-title" type="text" value="'.$title.'" /></p>';
	echo '<p style="text-align:right;"><label for="dkret-meta-login">' . __('Login on Posts page:', 'dkret') . ' <input id="dkret-meta-login" name="dkret-meta-login" type="checkbox" '. $dkretlogin .'  /></label></p>';
	echo '<input type="hidden" id="dkret-submit" name="dkret-submit" value="1" />';
}

$options = get_option('widget_dkret_meta');
if ( !is_array($options) )
	$options = array('dkretlogin'=>'checked="checked"',);
	update_option('widget_dkret_meta', $options);

$widget_ops = array('classname' => 'widget_dkret_meta', 'description' => __( "Log in/out links (dkret)", 'dkret') );
wp_register_sidebar_widget('meta', __('Meta','dkret'), 'widget_dkret_meta', $widget_ops);
unregister_widget_control('meta');
wp_register_widget_control('meta', __('Meta','dkret'), 'widget_control_dkret_meta');
?>
